// pages/img/down.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rewardId:1,
    water:0,
    rewarded:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    var id = options.id;
    this.setData({
        id:id
    })
    this.getDetail(id);
    this.setData({
        domain:app.globalData.domain,
        width:app.globalData.windowWidth,
        height:app.globalData.windowHeight,
        safeHeight:app.globalData.safeHeight,
        statusBarHeight:app.globalData.statusBarHeight,
        navHeight:app.globalData.navHeight,
        marginTop:app.globalData.navTitleTop
      });
  },
  getDetail: function (id){
    wx.showLoading({							
      title: '加载中...',
    })
    var that = this;
    var page=this.data.page;
    wx.request({
      url: app.globalData.domain+'wechat/face/getImageDetail',
      data: {
          id:id,
         // page:page
    },
      success:function(res){
        wx.hideLoading();
        console.log(res);
        var data= res.data;
        that.setData({
            img:data.img,
            reward:data.reward,
            water:data.water,
            rewarded:data.rewarded,
            rewardWater:data.rewardWater,
            ImgUser:data.imgUser
          //swiperList: contentlist.swiperList
        })
        ////console.log(that.data.matchListData)
      },
      fail:function(){
      }
    })
  },
  rewardWater:function(){
    var that=this;
    //var order_id=this.data.order_id;
    var open = wx.getStorageSync('open');
    //var rewardId = this.data.rewardId;
    //var reward = this.data.reward;
    var money = this.data.rewardWater;
    var img = this.data.img;
    let water = this.data.water
    if(water == 1){
        wx.showModal({
          title: '重要提示',
          content: '该图片您已经去水印了,您可以直接下载该图片',
          complete: (res) => {
            if (res.cancel) {
              
            }
        
            if (res.confirm) {
              
            }
          }
        })
    }else{
        wx.request({
            url:app.globalData.domain+'wechat/face/reward',    
            data: {order_id: img.id, open_id: open.id, openid: open.mini_openid,money:money,type:4},
            success: function (res) {
             console.log(res.data);
              //console.log('调起支付');
              var code=res.data.code;
              if(code==200){
              var data=res.data.data;
                wx.requestPayment({
                    'timeStamp': data.timeStamp,
                    'nonceStr': data.nonceStr,
                    'package': data.package,
                    'signType': 'MD5',
                    'paySign': data.paySign,
                    'success': function (res) {
                     // console.log('success');
                      wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 3000
                      });
                      that.setData({
                          water:1
                      })
                      //that.waitPayShow();
                    },
                    'fail': function (res) {
                      //console.log(res);
                    },
                    'complete': function (res) {
                     // console.log('complete');
                    }
                  });
              }else{
        
              }
          
            },
            fail: function (res) {
              //console.log("fail")
            }
          });
    }
     
  
  },
  reward:function(){
    var that=this;
    var rewarded =this.data.rewarded;
    if(rewarded == 1){
        wx.showModal({
          title: '友情提示',
          content: '您已经打赏过了，继续打赏么？',
          complete: (res) => {
            if (res.cancel) {
              return ;
            }
        
            if (res.confirm) {
              that.gotoReward();
            }
          }
        })
    }
    //var order_id=this.data.order_id;
  
  
  },
  gotoReward(){
    var that=this;
    var open = wx.getStorageSync('open');
    var rewardId = this.data.rewardId;
    var reward = this.data.reward;
    var money=reward[rewardId];
    var img = this.data.img;
    wx.request({
      url:app.globalData.domain+'wechat/face/reward',    
      data: {order_id: img.id, open_id: open.id, openid: open.mini_openid,money:money,type:3},
      success: function (res) {
       console.log(res.data);
        //console.log('调起支付');
        var code=res.data.code;
        if(code==200){
        var data=res.data.data;
          wx.requestPayment({
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,
              'success': function (res) {
               // console.log('success');
                wx.showToast({
                  title: '支付成功',
                  icon: 'success',
                  duration: 3000
                });
                
                //that.waitPayShow();
              },
              'fail': function (res) {
                //console.log(res);
              },
              'complete': function (res) {
               // console.log('complete');
              }
            });
        }else{
  
        }
    
      },
      fail: function (res) {
        //console.log("fail")
      }
    }); 
  },
  selectReward(e){
      console.log(e);
    var index = e.currentTarget.dataset.index;
    this.setData({
        rewardId:index
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  goBack:function(){
    wx.navigateBack();
  },
  down() {
    let that = this
    //若二维码未加载完毕，加个动画提高用户体验
    wx.showToast({
     icon: 'loading',
     title: '正在保存图片',
     duration: 1000
    })
    //判断用户是否授权"保存到相册"
    wx.getSetting({
     success (res) {
      //没有权限，发起授权
      if (!res.authSetting['scope.writePhotosAlbum']) {
       wx.authorize({
        scope: 'scope.writePhotosAlbum',
        success () {//用户允许授权，保存图片到相册
         that.savePhoto();
        },
        fail () {//用户点击拒绝授权，跳转到设置页，引导用户授权
         wx.openSetting({
          success () {
           wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
             that.savePhoto();
            }
           })
          }
         })
        }
       })
      } else {//用户已授权，保存到相册
       that.savePhoto()
      }
     }
    })
   },
  //保存图片到相册，提示保存成功
   savePhoto() {
    let that = this
    
    var water =this.data.water;
    if(water==1){
        var url = this.data.img.image;
    }else{
        var url = this.data.img.thumb;
    }
    console.log(url);
    wx.downloadFile({
     url: url,
     success: function (res) {
      wx.saveImageToPhotosAlbum({
       filePath: res.tempFilePath,
       success(res) {
        wx.showToast({
         title: '保存成功',
         icon: "success",
         duration: 1000
        })
       }
      })
     }
    })
   },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})